package com.cunfang.entrance;

import java.io.IOException;
import java.lang.reflect.Method;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.BeansException;
import org.springframework.stereotype.Controller;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cunfang.base.controller.BaseController;
import com.cunfang.entity.PageData;
import com.cunfang.util.SpringContextUtil;



/**
 * 
 * @author Administrator 系统路由
 */
@ResponseBody
@Controller
@RequestMapping(value = "router")
public class Router extends BaseController {

	/**
	 * 系统API总入口
	 */
	@RequestMapping(value = "/rest")
	public void rest(HttpServletRequest request, HttpServletResponse response) {
		try {
			PageData pd = this.getPageData();
			if (!pd.containsKey("method")) {
				this.outError("API接口名称,必填");
				return;
			}
			if (pd.get("method").toString().length() <= 0) {
				this.outError("API接口method参数,必填");
				return;
			}
			// API接口全称
			String methodAPI = pd.get("method").toString();

			// API方法名字
			String methodName = methodAPI
					.substring(methodAPI.lastIndexOf(".") + 1);

			// API去掉方法名字的接口名称
			String classAPI = methodAPI
					.substring(0, methodAPI.lastIndexOf("."));
			// 类名称
			String className = "com.cunfang." + classAPI;

			// 获取Bean
			Object serviceObjec = SpringContextUtil.getBean(className);

			// 1、获取带参类型为PageData的方法
			Method method = ReflectionUtils.findMethod(serviceObjec.getClass(),
					methodName, PageData.class);
			if (method != null) {
				Object returnObj = ReflectionUtils.invokeMethod(method,
						serviceObjec, pd);
				if (returnObj != null) {
					if (method.getReturnType().equals(String.class)) {
						String str = (String) returnObj;
						this.outTString(str);
					}
				} else {
					this.outString("");
				}
				return;
			}
			// 2、获取不带参数的方法
			method = ReflectionUtils.findMethod(serviceObjec.getClass(),
					methodName);
			if (method != null) {
				Object returnObj = ReflectionUtils.invokeMethod(method,
						serviceObjec);
				if (returnObj != null) {
					if (method.getReturnType().equals(String.class)) {
						String str = (String) returnObj;
						this.outTString(str);
					}
				} else {
					this.outString("");
				}
				return;
			}

			// 3、获取带参类型为HttpServletRequest的方法
			method = ReflectionUtils.findMethod(serviceObjec.getClass(),
					methodName, HttpServletRequest.class);
			if (method != null) {
				Object returnObj = ReflectionUtils.invokeMethod(method,
						serviceObjec, request);
				if (returnObj != null) {
					if (method.getReturnType().equals(String.class)) {
						String str = (String) returnObj;
						this.outTString(str);
					} else if (method.getReturnType().equals(
							ServletOutputStream.class)) {
						this.outByteArray((ServletOutputStream) returnObj);
					}
				} else {
					this.outString("");
				}
				return;
			}

			// 4、获取带参类型为HttpServletResponse的方法
			method = ReflectionUtils.findMethod(serviceObjec.getClass(),
					methodName, HttpServletResponse.class);
			if (method != null) {
				ReflectionUtils.invokeMethod(method, serviceObjec, response);
			} else {
				this.outError("找不到对应的方法");
			}
		} catch (BeansException e) {
			this.outError("找不到对应类");
			e.printStackTrace();
		} catch (IOException e) {
			this.outError("参数格式异常");
			e.printStackTrace();
		} catch (Exception e) {
			this.outError("系统异常");
			e.printStackTrace();
		}
	}
}
